package servlet;

import com.alibaba.fastjson.JSON;
import polo.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/UserServlet.do")
public class UserSevlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String flag = req.getParameter("flag");
        if(flag.equals("getData")){
            getData(req,resp);
        }else if(flag.equals("addUser")){
            addUser(req,resp);
        }
    }

    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user=this.createUser(req);
        ServletContext servletContext=this.getServletContext();
        List<User> list=(List<User>)servletContext.getAttribute("list");
        list.add(user);
        resp.setContentType("text/plain;charset=utf-8");
        PrintWriter writer=resp.getWriter();
        writer.println("添加成功");
        writer.flush();
        writer.close();
    }

    //  返回用户对象
    private User createUser(HttpServletRequest request){
        String userid=request.getParameter("userid");
        String username=request.getParameter("username");
        String salary=request.getParameter("salary");
        String birthday=request.getParameter("birthday");
        User user = new User();
        user.setUserName(username);
        user.setId(userid);
        user.setBirthday(birthday);
        user.setSalary(Double.parseDouble(salary));
        return user;
    }


    //  获取用户数据
    private void getData(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<User> list = (List<User>)this.getServletContext().getAttribute("list");
        String s= JSON.toJSONString(list);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        writer.println(s);
        writer.flush();
        writer.close();
    }

    @Override
    public void init() throws ServletException {
        ArrayList<User> list = new ArrayList<>();
        User user1 = new User("1", "zhangsan", "1225", 1000.00);
        User user2 = new User("2", "李四", "1225", 3000.00);
        User user3 = new User("3", "王五", "1225", 1000.00);
        User user4 = new User("4", "萍萍", "1225", 5000.00);
        User user5 = new User("5", "团团", "1225", 6000.00);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);

        ServletContext context=this.getServletContext();
        context.setAttribute("list",list);
    }
}
